package com.trdcore.zyzkt.controller;

import com.trdcore.zyzkt.resp.ResultData;
import com.trdcore.zyzkt.resp.ReturnCodeEnum;
import com.trdcore.zyzkt.service.VodService;
import com.trdcore.zyzkt.utils.ConstantPropertiesUtil;
import com.trdcore.zyzkt.utils.Signature;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

@Tag(name = "腾讯云点播")
@RestController
@RequestMapping("/admin/vod")
public class VodController {

    @Autowired
    private VodService vodService;


    //返回客户端上传视频签名
    @GetMapping("/sign")
    public ResultData sign() {
        Signature sign = new Signature();
        // 设置 App 的云 API 密钥
        sign.setSecretId(ConstantPropertiesUtil.ACCESS_KEY_ID);
        sign.setSecretKey(ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        sign.setCurrentTime(System.currentTimeMillis() / 1000);
        sign.setRandom(new Random().nextInt(Integer.MAX_VALUE));
        sign.setSignValidDuration(3600 * 24 * 2); // 签名有效期：2天
        try {
            String signature = sign.getUploadSignature();
            System.out.println("signature : " + signature);
            return ResultData.success(signature);
        } catch (Exception e) {
            System.out.print("获取签名失败");
            e.printStackTrace();
            return ResultData.fail(ReturnCodeEnum.RC999.getCode(), "获取签名失败");
        }
    }
//
//    //上传视频
//    @PostMapping("/upload")
//    @Operation(summary = "上传视频",description = "上传视频")
//    public ResultData uploadVideo(
//            @Parameter(name = "file", description = "文件", required = true)
//            @RequestParam("file") MultipartFile file) throws IOException {
//        InputStream inputStream = file.getInputStream();
//        String originalFilename = file.getOriginalFilename();
//        String videoId = vodService.uploadVideo(inputStream, originalFilename);
//        return ResultData.success(videoId);
//    }

    //删除视频
    @DeleteMapping("remove/{videoSourceId}")
    @Operation(summary = "删除视频",description = "删除视频")
    public ResultData removeVideo( @PathVariable("videoSourceId") String videoSourceId) throws Exception {
        vodService.removeVideo(videoSourceId);
        return ResultData.success(null);
    }
}
